package services;

import java.sql.SQLException;

public class UserServices {
        
        public Integer registration(String login, String password) {
        	System.out.println("registration");
                Integer retValule = 0;
                if(login==null || !login.matches("[a-zA-Z]{1}[a-zA-Z\\d_ ]*"))
                        return -1;
                if(password==null || !password.matches("[a-zA-Z\\d_.^%&@#?]{7}[a-zA-Z\\d_.^&%@#?]*"))
                        return -2;
                try {
                        User ret = DBConnector.findClient(login);
                if(ret.getId()!=-1)
                        return -3;
                
                retValule = DBConnector.addClient(new User(login), password);
                return retValule;
                
                } catch (ClassNotFoundException | SQLException e) {
                        e.printStackTrace();
                } 
                return retValule;       
        }
        public User getUserById(Integer id) {
                if(id==null ||  id == 0)
                        return null;
                try {
                        return DBConnector.findClientById(id);
                } catch (ClassNotFoundException | SQLException e) {
                        e.printStackTrace();
                }
                return null;
        }
        public User authorisation(String login, String password) {
        	System.out.println("authorisation");
                if(login==null || !login.matches("[a-zA-Z]{1}[a-zA-Z\\d_ ]*"))
                        return null;
                if(password==null || !password.matches("[a-zA-Z\\d_.^%&@#?]{7}[a-zA-Z\\d_.^&%@#?]*"))
                        return null;
                try {
                        User ret = DBConnector.findClient(login);
                        if(ret.getId()==-1)
                                return null;
                        
                        if(DBConnector.checkPassword(ret.getId(), password))
                                return ret;
                } catch (ClassNotFoundException | SQLException e) {
                        e.printStackTrace();
                }
                return null;    
        }
}